﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports BusinessRule.IBusiness
Imports DatabaseAccess.IDAL
Imports System.Data
Imports BusinessRule.Common
Imports DatabaseAccess.DALFactory
Imports External.APICalling
Imports BusinessRule.DTO
Imports DatabaseAccess.DBUtility

Public Class uMESScrapInfoBusiness
    Implements uMESScrapInfoIBusiness
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()

    ''' <summary>
    ''' 根据批次和工序获取报废信息
    ''' </summary>
    ''' <returns></returns>
    Private Function GetScrapInfo(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESScrapInfoIBusiness.GetScrapInfo
        Dim oResult As New uMESProcessResult

        Try
            Dim strSQL, strContainerName, strSpecName, strSpecRev, strQualityRecordName As String
            strContainerName = ""
            If QueryCondition.Keys.Contains("ContainerName") = True Then
                strContainerName = QueryCondition("ContainerName").ToString()
            End If
            strSpecName = ""
            If QueryCondition.Keys.Contains("SpecName") = True Then
                strSpecName = QueryCondition("SpecName").ToString()
            End If

            strSpecRev = ""
            If QueryCondition.Keys.Contains("SpecRev") = True Then
                strSpecRev = QueryCondition("SpecRev").ToString()
            End If

            strQualityRecordName = ""
            If QueryCondition.Keys.Contains("QualityRecordName") = True Then
                strQualityRecordName = QueryCondition("QualityRecordName").ToString()
            End If

            strSQL = "SELECT Scrapinfo.Scrapinfoid, Scrapinfo.Scrapinfoname, Container.Containername, Specbase.Specname, Scrapinfo.Scrapqty," & vbCrLf & _
                    "       Employee.Fullname, Scrapinfo.Submitdate, Scrapinfo.Notes" & vbCrLf & _
                    "FROM Scrapinfo" & vbCrLf & _
                    "INNER JOIN Container ON Scrapinfo.Containerid = Container.Containerid" & vbCrLf & _
                    "INNER JOIN Spec ON Scrapinfo.Specid = Spec.Specid" & vbCrLf & _
                    "INNER JOIN Specbase ON Spec.Specbaseid = Specbase.Specbaseid" & vbCrLf & _
                    "INNER JOIN Employee ON Employee.Employeeid = Scrapinfo.Submitemployeeid" & vbCrLf & _
                    "LEFT OUTER JOIN Qualityrecord ON Qualityrecord.Qualityrecordid = Scrapinfo.Qualityrecordid" & vbCrLf & _
                    "WHERE 1=1" & vbCrLf
            If strContainerName <> "" Then
                strSQL = strSQL & "AND Container.ContainerName = '" & strContainerName & "'" & vbCrLf
            End If
            If strSpecName <> "" Then
                strSQL = strSQL & "AND Specbase.Specname = '" & strSpecName & "'" & vbCrLf
            End If
            If strSpecRev <> "" Then
                strSQL = strSQL & "AND Spec.Specrevision = '" & strSpecRev & "'" & vbCrLf
            End If
            If strQualityRecordName <> "" Then
                strSQL = strSQL & "AND Qualityrecord.Qualityrecordname = '" & strQualityRecordName & "'" & vbCrLf
            End If

            strSQL = strSQL & "ORDER BY Scrapinfo.Submitdate" & vbCrLf

            Dim dtMain As DataTable = dal.GetCheckTempInfo(strSQL)
            dtMain = DataAdjust.FormatDataSetSpecNameByColumName("SpecName", dtMain)

            strSQL = "SELECT pi.Productno" & vbCrLf & _
                "FROM Qualityrecord qr" & vbCrLf & _
                "INNER JOIN Qualityrecordproductnoinfo qrpi ON qr.Qualityrecordid = qrpi.Qualityrecordid" & vbCrLf & _
                "INNER JOIN Productnoinfo pi ON qrpi.Productnoinfoid = pi.Productnoinfoid" & vbCrLf & _
                "LEFT OUTER JOIN " & vbCrLf & _
                "(" & vbCrLf & _
                "SELECT DISTINCT sd.productno" & vbCrLf & _
                "FROM Scrapinfo s" & vbCrLf & _
                "INNER JOIN Scrapdetails sd ON sd.Scrapinfoid = s.Scrapinfoid" & vbCrLf & _
                ") Cscrap" & vbCrLf & _
                "ON Cscrap.productno =  pi.Productno" & vbCrLf & _
                "LEFT JOIN qualitydisposeadvice qd ON qd.qualitydisposeadviceid = pi.qualitydisposeadviceid" & vbCrLf & _
                "WHERE pi.Productno <> '00000000' AND Cscrap.productno IS NULL" & vbCrLf & _
                "AND qd.qualitydisposeadvicename LIKE '%报废%'" & vbCrLf
            If strQualityRecordName <> "" Then
                strSQL = strSQL & "AND qr.Qualityrecordname = '" & strQualityRecordName & "'" & vbCrLf
            Else
                strSQL = strSQL & "AND qr.Qualityrecordname IS NULL" & vbCrLf
            End If
            strSQL = strSQL & "ORDER BY pi.Productno" & vbCrLf

            Dim dtContainerChild As DataTable = dal.GetCheckTempInfo(strSQL)

            Dim ds As New DataSet
            ds.Tables.Add(dtMain)
            ds.Tables.Add(dtContainerChild)
            ds.Tables(0).TableName = "dtMain"
            ds.Tables(1).TableName = "dtContainerChild"

            oResult.DBDataSet = ds
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

    ''' <summary>
    ''' 获取当天最大流水号的报废单
    ''' </summary>
    ''' <returns></returns>
    Private Function GetLastScrapInfo(ByVal QueryCondition As Dictionary(Of String, String)) As String Implements uMESScrapInfoIBusiness.GetLastScrapInfo
        Dim oResult As New uMESProcessResult
        Dim strSQL, strPrefix As String
        GetLastScrapInfo = ""
        strSQL = "SELECT Scrapinfo.Scrapinfoname" & vbCrLf & _
                "FROM Scrapinfo" & vbCrLf & _
                "WHERE Scrapinfo.Scrapinfoname LIKE '" & Now.ToString("yyyyMMdd") & "%'" & vbCrLf & _
                "ORDER BY Scrapinfo.Scrapinfoname DESC" & vbCrLf

        Dim dtMain As DataTable = dal.GetQualityRecordInfoForProductNo(strSQL)
        strPrefix = Now.ToString("yyyyMMdd") & "_"
        If dtMain.Rows.Count > 0 Then
            Dim strLastScrapInfoName, strIndex As String
            Dim intIndex As Integer
            strLastScrapInfoName = dtMain.Rows(0).Item("Scrapinfoname")
            intIndex = CInt(Trim(strLastScrapInfoName).Replace(strPrefix, ""))
            strIndex = Format(intIndex + 1, "00000")
            GetLastScrapInfo = strPrefix & strIndex
        Else
            GetLastScrapInfo = strPrefix & "00001"
        End If
    End Function

    ''' <summary>
    ''' 创建报废单信息
    ''' </summary>
    ''' <param name="oScrapInfo">创建报废单所需的基础信息</param>
    ''' <param name="result">执行结果</param>
    ''' <param name="message">返回结果信息</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CreateScrapInfo(ByVal oScrapInfo As uMESScrapInfoDTO, ByRef result As Boolean, ByRef message As String) As Boolean Implements uMESScrapInfoIBusiness.CreateScrapInfo
        CreateScrapInfo = False
        result = False
        Try
            Dim strInfo As String = ""
            Dim success As Boolean = False
            Dim daServerHost As ServerHostInfo = oScrapInfo.ServerHostInfo

            Dim strTxnDocName, strTxnName, strEventName As String
            strTxnDocName = "ScrapInfoMaintDoc"
            strTxnName = "ScrapInfoMaint"
            strEventName = "New"

            Dim m_DataList As New List(Of ClientAPIEntity)

            Dim dataEntity As New ClientAPIEntity

            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Name"
            dataEntity.ClientDataValue = oScrapInfo.ScrapInfoName
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Notes"
            dataEntity.ClientDataValue = oScrapInfo.Notes
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.ContainerField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Container"
            dataEntity.ClientDataValue = oScrapInfo.ContainerName
            dataEntity.ClientOtherValue = ""
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            Dim strSpecName As String = oScrapInfo.SpecName
            Dim strSpecRev As String = oScrapInfo.SpecRevision

            dataEntity.ClientDataTypeEnum = DataTypeEnum.RevisionedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Spec"
            dataEntity.ClientDataValue = strSpecName
            If strSpecRev <> "" Then
                dataEntity.ClientDataIsROR = False
                dataEntity.ClientDataVersion = strSpecRev
            Else
                dataEntity.ClientDataIsROR = True
                dataEntity.ClientDataVersion = strSpecRev
            End If
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "ScrapQty"
            dataEntity.ClientDataValue = oScrapInfo.ScrapQty
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "LossReason"
            dataEntity.ClientDataValue = oScrapInfo.LossReason
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "QualityRecord"
            dataEntity.ClientDataValue = oScrapInfo.QualityRecordName

            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "SubmitEmployee"
            dataEntity.ClientDataValue = oScrapInfo.SubmitEmployee
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "SubmitDate"
            dataEntity.ClientDataValue = oScrapInfo.SubmitDate
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Description"
            dataEntity.ClientDataValue = oScrapInfo.Description
            m_DataList.Add(dataEntity)

            Dim m_ChildList As New Dictionary(Of String, List(Of ClientAPIEntity))

            ''零件编号信息
            Dim p_dataProductList As New Dictionary(Of String, List(Of ClientAPIEntity))

            Dim DT As DataTable = oScrapInfo.ProductNoInfo

            If DT IsNot Nothing Then
                For i As Integer = 0 To DT.Rows.Count - 1
                    Dim dataListItem As New List(Of ClientAPIEntity)
                    Dim dataItem As New ClientAPIEntity
                    dataItem.ClientDataName = "ProductNo"
                    dataItem.ClientDataValue = "000000"
                    dataItem.ClientDataTypeEnum = DataTypeEnum.DataField
                    dataListItem.Add(dataItem)

                    dataItem = New ClientAPIEntity
                    dataItem.ClientDataName = "Qty"
                    dataItem.ClientDataValue = DT.Rows(i)("Qty").ToString
                    dataItem.ClientDataTypeEnum = DataTypeEnum.DataField
                    dataListItem.Add(dataItem)

                    dataItem = New ClientAPIEntity
                    dataItem.ClientDataName = "LossReason"
                    dataItem.ClientDataValue = DT.Rows(i)("LossReason").ToString
                    dataItem.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
                    dataListItem.Add(dataItem)
                    p_dataProductList.Add(i, dataListItem)
                Next
            Else
                'Dim dataListItem As New List(Of ClientAPIEntity)
                'Dim dataItem As New ClientAPIEntity
                'dataItem.ClientDataName = "ProductNo"
                'dataItem.ClientDataValue = "000000"
                'dataItem.ClientDataTypeEnum = DataTypeEnum.DataField
                'dataListItem.Add(dataItem)

                'dataItem = New ClientAPIEntity
                'dataItem.ClientDataName = "Qty"
                'dataItem.ClientDataValue = oScrapInfo.ScrapQty
                'dataItem.ClientDataTypeEnum = DataTypeEnum.DataField
                'dataListItem.Add(dataItem)

                'dataItem = New ClientAPIEntity
                'dataItem.ClientDataName = "LossReason"
                'dataItem.ClientDataValue = oScrapInfo.LossReason
                'dataItem.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
                'dataListItem.Add(dataItem)
                'p_dataProductList.Add(0, dataListItem)
            End If


            Dim strChildContainer As String = "ScrapDetails"

            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim clientData As New CamstarClientAPI(serverUser, serverPassword)
            'success = clientData.CreateModelHasSubentityList(strTxnDocName, strTxnName, strEventName, m_DataList, strChildContainer, p_dataProductList, strInfo)

            success = clientData.CreateModelByStandard(strTxnDocName, strTxnName, strEventName, m_DataList, strInfo)
            If success = True Then
                CreateScrapInfo = True
                result = True
            End If

            message = strInfo
        Catch ex As Exception
            result = False
            message = ex.Message
            Return False
        End Try
    End Function

    ''' <summary>
    ''' 修改零件序号状态
    ''' </summary>
    ''' <param name="oScrapInfoDTO">修改零件序号状态所需的基础信息</param>
    ''' <param name="result">执行结果</param>
    ''' <param name="message">返回结果信息</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function UpdateProductNoStatus(ByVal oScrapInfoDTO As uMESScrapInfoDTO, ByRef result As Boolean, ByRef message As String) As Boolean Implements uMESScrapInfoIBusiness.UpdateProductNoStatus
        UpdateProductNoStatus = False
        result = False
        Try
            Dim strInfo As String = ""
            Dim success As Boolean = False
            Dim daServerHost As ServerHostInfo = oScrapInfoDTO.ServerHostInfo

            Dim strTxnDocName, strTxnName, strEventName, strSubentityListName As String
            strTxnDocName = "ContainerProductNoMaintDoc"
            strTxnName = "ContainerProductNoMaint"
            strEventName = "Load"
            strSubentityListName = "ProductListItem"

            Dim strUpdateName, strUpdateVersion, strUpdateCDOType As String
            strUpdateName = oScrapInfoDTO.ContainerName
            strUpdateVersion = ""
            strUpdateCDOType = "NDO"

            Dim m_DataList As New List(Of ClientAPIEntity)

            Dim dataEntity As New ClientAPIEntity

            Dim m_ChildList As New Dictionary(Of String, List(Of ClientAPIEntity))

            Dim dtChildList As DataTable = oScrapInfoDTO.ProductNoInfo

            If dtChildList IsNot Nothing Then
                If dtChildList.Rows.Count > 0 Then
                    For i As Integer = 0 To dtChildList.Rows.Count - 1
                        Dim m_Child As New List(Of ClientAPIEntity)
                        Dim stepEntity As New ClientAPIEntity

                        stepEntity.ClientDataTypeEnum = DataTypeEnum.DataField
                        stepEntity.ClientDataName = "Name"
                        stepEntity.ClientDataValue = dtChildList.Rows(i).Item("ProductNo").ToString
                        m_Child.Add(stepEntity)

                        stepEntity = New ClientAPIEntity
                        stepEntity.ClientDataTypeEnum = DataTypeEnum.DataField
                        stepEntity.ClientDataName = "IsLoss"
                        stepEntity.ClientDataValue = 1
                        m_Child.Add(stepEntity)

                        m_ChildList.Add(i.ToString, m_Child)
                    Next
                End If
            End If

            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim clientData As New CamstarClientAPI(serverUser, serverPassword)
            success = clientData.UpdateModelHasNamedSubentityList(strTxnDocName, strTxnName, strUpdateName, strUpdateVersion, _
                                                            strUpdateCDOType, m_DataList, strSubentityListName, m_ChildList, strInfo)

            If success = True Then
                UpdateProductNoStatus = True
                result = True
            End If

            message = strInfo
        Catch ex As Exception
            result = False
            message = ex.Message
            Return False
        End Try
    End Function

    ''' <summary>
    ''' 根据报废单ID获取报废信息
    ''' </summary>
    ''' <returns></returns>
    Private Function GetScrapInfoDetail(ByVal QueryCondition As Dictionary(Of String, String)) As uMESProcessResult Implements uMESScrapInfoIBusiness.GetScrapInfoDetail
        Dim oResult As New uMESProcessResult

        Try
            Dim strScrapInfoid As String = ""
            If QueryCondition.Keys.Contains("ScrapInfoid") = True Then
                strScrapInfoid = QueryCondition("ScrapInfoid").ToString()
            End If

            Dim strSQL As String = "SELECT sc.scrapinfoid,sc.scrapinfoname,c.containername,sb.specname,sc.scrapqty," & vbCrLf & _
                                    "   e.fullname,sc.submitdate,sd.qty,sd.productno,sd.notes,lr.lossreasonname," & vbCrLf & _
                                    "   (REPLACE(sd.productno,c.containername || '-','')) AS productnoDisplay" & vbCrLf & _
                                    "FROM scrapdetails sd" & vbCrLf & _
                                    "LEFT JOIN scrapinfo sc ON sc.scrapinfoid = sd.scrapinfoid" & vbCrLf & _
                                    "LEFT JOIN container c ON c.containerid = sc.containerid" & vbCrLf & _
                                    "LEFT JOIN spec s ON s.specid = sc.specid" & vbCrLf & _
                                    "LEFT JOIN specbase sb ON sb.specbaseid = s.specbaseid" & vbCrLf & _
                                    "LEFT JOIN employee e ON e.employeeid = sc.submitemployeeid" & vbCrLf & _
                                    "LEFT JOIN qualityrecord qr ON qr.qualityrecordid = sc.qualityrecordid" & vbCrLf & _
                                    "LEFT JOIN lossreason lr ON lr.lossreasonid = sd.lossreasonid" & vbCrLf & _
                                    "WHERE 1=1" & vbCrLf
            If strScrapInfoid <> "" Then
                strSQL += "AND sc.scrapinfoid = '" & strScrapInfoid & "'" & vbCrLf
            End If

            Dim dtMain As DataTable = dal.GetCheckTempInfo(strSQL)
            dtMain = DataAdjust.FormatDataSetSpecNameByColumName("SpecName", dtMain)

            oResult.DBTable = dtMain
            oResult.Result = True
            Return oResult
        Catch ex As Exception
            oResult.Result = False
            oResult.Message = ex.Message
            Return oResult
        End Try
    End Function

#Region "修改批次数量"
    ''' <summary>
    ''' 修改批次数量
    ''' </summary>
    ''' <param name="oChangeQtyDTO">修改批次数量所需的基础信息</param>
    ''' <param name="result">执行结果</param>
    ''' <param name="message">返回结果信息</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ChangeQty(ByVal oChangeQtyDTO As uMESChangeQtyDTO, ByRef result As Boolean, ByRef message As String) As Boolean Implements uMESScrapInfoIBusiness.ChangeQty 'ByVal Container As String, ByVal ChangeQtyType As String, ByVal Qty As String, ByVal ReasonCode As String) As Boolean
        ChangeQty = False
        result = False
        Try
            Dim strInfo As String = ""
            Dim success As Boolean = False
            Dim daServerHost As ServerHostInfo = oChangeQtyDTO.ServerHostInfo

            Dim strTxnDocName, strTxnName As String
            strTxnDocName = "ChangeQtyDoc"
            strTxnName = "ChangeQty"

            Dim m_DataList As New List(Of ClientAPIEntity)

            Dim dataEntity As New ClientAPIEntity

            dataEntity.ClientDataTypeEnum = DataTypeEnum.ContainerField
            dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
            dataEntity.ClientDataName = "Container"
            dataEntity.ClientDataValue = oChangeQtyDTO.ContainerName
            dataEntity.ClientOtherValue = ""
            m_DataList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataTypeEnum = DataTypeEnum.DataField
            dataEntity.ClientDataName = "CloseWhenZero"
            dataEntity.ClientDataValue = oChangeQtyDTO.CloseWhenZero
            m_DataList.Add(dataEntity)

            Dim m_ChildList As New Dictionary(Of String, List(Of ClientAPIEntity))

            Dim dtChildList As DataTable = oChangeQtyDTO.ChangeQtyDetail

            If dtChildList IsNot Nothing Then
                If dtChildList.Rows.Count > 0 Then
                    For i As Integer = 0 To dtChildList.Rows.Count - 1
                        Dim m_Child As New List(Of ClientAPIEntity)
                        Dim stepEntity As New ClientAPIEntity

                        stepEntity.ClientDataTypeEnum = DataTypeEnum.DataField
                        dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
                        stepEntity.ClientDataName = "Qty"
                        stepEntity.ClientDataValue = dtChildList.Rows(i).Item("Qty").ToString
                        m_Child.Add(stepEntity)

                        stepEntity = New ClientAPIEntity
                        stepEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
                        dataEntity.ClientInputTypeEnum = InputTypeEnum.Details
                        stepEntity.ClientDataName = "ReasonCode"
                        stepEntity.ClientDataValue = dtChildList.Rows(i).Item("ReasonCode").ToString
                        m_Child.Add(stepEntity)
                        m_ChildList.Add(i.ToString, m_Child)
                    Next
                End If
            End If

            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim clientData As New CamstarClientAPI(serverUser, serverPassword)

            success = clientData.ContainerChangeQty(strTxnDocName, strTxnName, m_DataList, "ServiceDetails", m_ChildList, _
                                                    oChangeQtyDTO.ChangeQtyType, strInfo)

            If success = True Then
                ChangeQty = True
                result = True
            End If

            message = strInfo
        Catch ex As Exception
            result = False
            message = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "关闭批次"
    Public Function CloseContainer(ByVal oChangeQtyDTO As uMESChangeQtyDTO, ByVal strContainerName As String) As String Implements uMESScrapInfoIBusiness.CloseContainer

        Dim strSql As String = String.Format("SELECT c.qty FROM container c WHERE c.containername='{0}'", strContainerName)
        Dim tmpTable As DataTable = dal.GetQualityRecordInfoForProductNo(strSql)
        If tmpTable.Rows.Count = 0 Then
            Return ""
        End If
        Dim strResult = ""
        If Convert.ToInt32(tmpTable.Rows(0)("qty")) = 0 Then
            Dim strCloseReason = "报废批次关闭"
            Dim dataEntityList As New List(Of ClientAPIEntity)
            Dim dataEntity As New ClientAPIEntity
            dataEntityList.Clear()
            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataName = "Container"
            dataEntity.ClientDataValue = strContainerName
            dataEntity.ClientDataTypeEnum = DataTypeEnum.ContainerField
            dataEntity.ClientOtherValue = "Lot"
            dataEntityList.Add(dataEntity)

            dataEntity = New ClientAPIEntity
            dataEntity.ClientDataName = "ChangeStatusReason"
            dataEntity.ClientDataValue = strCloseReason
            dataEntity.ClientDataTypeEnum = DataTypeEnum.NamedObjectField
            dataEntityList.Add(dataEntity)

            Dim strMessage = "", success = False

            Dim daServerHost As ServerHostInfo = oChangeQtyDTO.ServerHostInfo
            Dim serverUser As String = daServerHost.UserName
            Dim serverPassword As String = daServerHost.Password

            Dim api As New CamstarClientAPI(serverUser, serverPassword)
            success = api.RunTxnService("CloseDoc", "Close", dataEntityList, strMessage)
            If success = False Then
                strResult = strMessage
            End If
        End If
        Return strResult
    End Function
#End Region

#Region "根据质量记载单统计累计报废数量"
    Public Function GetTotalScarpCount(ByVal oQualityRecordName As String) As Integer Implements uMESScrapInfoIBusiness.GetTotalScarpCount
        Dim toalScrapQty As Integer = 0
        Try
            If oQualityRecordName <> "" Then
                Dim strSql As String = String.Format("SELECT NVL(SUM(scrapqty),0) AS toalScrapQty FROM Scrapinfo JOIN QualityRecord ON Scrapinfo.Qualityrecordid = QualityRecord.Qualityrecordid WHERE QualityRecord.Qualityrecordname = '{0}'", oQualityRecordName)
                Dim dtMain As DataTable = DatabaseHelper.Query_ReturnDatTable(strSql)
                toalScrapQty = dtMain.Rows(0).Item("toalScrapQty")
            End If

            Return toalScrapQty
        Catch ex As Exception
            Return 0
        End Try
    End Function
#End Region

End Class

